package net.adst.ui.mainframe.tabs.ourTab;

import net.adst.resources.VariableNames;
import net.adst.searcher.Searcher;
import net.adst.util.DataManager;
import net.adst.util.GBC;
import net.adst.util.WiderDropDownCombo;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 6:56
 * To change this template use File | Settings | File Templates.
 */
public class SearchOurPanel extends JPanel {

    private JLabel needUpdateLabel;
    private JLabel namesTecLabel;
    private WiderDropDownCombo namesTecCombo;
    private JLabel surnameLabel;
    private WiderDropDownCombo surnameCombo;
    private JLabel customerLabel;
    private WiderDropDownCombo customerCombo;
    private JCheckBox paidFor;
    private JCheckBox unpaidFor;
    private JButton searchButton;

    public SearchOurPanel() {
        setLayout(new GridBagLayout());
        addSearchFields();
        addSearchButton();

    }

    private void addSearchFields() {
        Dimension dimForCombo = new Dimension(110, 20);
        needUpdateLabel = new JLabel("Для вывода результатов нажмите кнопку поиск");
        needUpdateLabel.setFont(new Font("Big", Font.PLAIN, 14));
        needUpdateLabel.setForeground(Color.RED);
        needUpdateLabel.setVisible(false);
        DataManager.setObject(VariableNames.UI_OUR_NEED_UPDATE_LABEL, needUpdateLabel);
        add(needUpdateLabel, new GBC(0, 0, 3, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        namesTecLabel = new JLabel("Выберете ед. техники:");
        namesTecCombo = new WiderDropDownCombo();
        namesTecCombo.setWide(true);
        namesTecCombo.setMaximumSize(dimForCombo);
        namesTecCombo.setPreferredSize(dimForCombo);
        namesTecCombo.setMinimumSize(dimForCombo);
        DefaultComboBoxModel namesTecModel = new DefaultComboBoxModel();
        namesTecCombo.setModel(namesTecModel);
        ActionListener needUpdateListner = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((JLabel) DataManager.getObject(VariableNames.UI_OUR_NEED_UPDATE_LABEL)).setVisible(true);
            }
        };

        namesTecCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_OUR_TECNAMECOMBO_MODEL, namesTecModel);
        DataManager.setObject(VariableNames.UI_OUR_TECNAMECOMBO, namesTecCombo);
        add(namesTecLabel, new GBC(0, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(namesTecCombo, new GBC(0, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));


        surnameLabel = new JLabel("Выберете фамилию:");
        surnameCombo = new WiderDropDownCombo();
        surnameCombo.setWide(true);
        surnameCombo.setMaximumSize(dimForCombo);
        surnameCombo.setPreferredSize(dimForCombo);
        surnameCombo.setMinimumSize(dimForCombo);
        DefaultComboBoxModel surnameModel = new DefaultComboBoxModel();
        surnameCombo.setModel(surnameModel);

        surnameCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_OUR_SURNAMECOMBO_MODEL, surnameModel);
        DataManager.setObject(VariableNames.UI_OUR_SURNAMECOMBO, surnameCombo);
        add(surnameLabel, new GBC(1, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(surnameCombo, new GBC(1, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));


        customerLabel = new JLabel("Выберете заказчика:");
        customerCombo = new WiderDropDownCombo();
        customerCombo.setWide(true);
        customerCombo.setMaximumSize(dimForCombo);
        customerCombo.setPreferredSize(dimForCombo);
        customerCombo.setMinimumSize(dimForCombo);
        DefaultComboBoxModel customerModel = new DefaultComboBoxModel();
        customerCombo.setModel(customerModel);

        customerCombo.addActionListener(needUpdateListner);
        DataManager.setObject(VariableNames.UI_OUR_CUSTOMERCOMBO_MODEL, customerModel);
        DataManager.setObject(VariableNames.UI_OUR_CUSTOMERCOMBO, customerCombo);
        add(customerLabel, new GBC(2, 1).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        add(customerCombo, new GBC(2, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));

        paidFor = new JCheckBox("оплачено");
        paidFor.setFocusPainted(false);
        paidFor.setSelected(true);
        unpaidFor = new JCheckBox("не оплачено");
        unpaidFor.setFocusPainted(false);
        unpaidFor.setSelected(true);
        DataManager.setObject(VariableNames.UI_OUR_PAIDFOR_CHEK, paidFor);
        DataManager.setObject(VariableNames.UI_OUR_UNPAIDFOR_CHEK, unpaidFor);
        add(paidFor, new GBC(3, 1).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));
        add(unpaidFor, new GBC(3, 2).setInsets(3, 5).setAnchor(GBC.SOUTHWEST));

    }

    private void addSearchButton() {
        searchButton = new JButton(new ImageIcon(ResourceManager.class.getResource("/images/search-icon.png")));
        searchButton.setFocusPainted(false);
        searchButton.setToolTipText("Поиск данных по введенным параметрам.");
        add(searchButton, new GBC(4, 1, 1, 3).setInsets(0, 5).setAnchor(GBC.SOUTHWEST));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((JLabel) DataManager.getObject(VariableNames.UI_OUR_NEED_UPDATE_LABEL)).setVisible(false);
                ((JLabel) DataManager.getObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL)).setVisible(false);
                Searcher.searchOur();
                Searcher.searchNaem();
            }
        }
        );
    }
}
